/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections;

namespace DNNContentBuilder.Configuration
{
	/// <summary>
	/// Summary description for FormletDataCollection.
	/// </summary>
	public class FormletDataCollection : CollectionBase
	{
		#region Member Variables
		private readonly SortedList dictionary = new SortedList();
		#endregion

		#region Constructors
		public FormletDataCollection()
		{
		}

		internal FormletDataCollection(FormletDataCollection items)
		{
			foreach (FormletData item in items)
			{
				this.Add(item.Name, item);
			}
		}
		#endregion

		#region Properties
		public FormletData this[int index]
		{
			get
			{
				return (FormletData)this.List[index];
			}
			set
			{
				this.List[index] = value;
			}
		}

		public FormletData this[string name]
		{
			get
			{
				FormletData item = (FormletData)this.dictionary[name];
				return item;
			}
		}
		#endregion

		#region Implementation
		/// <summary>
		/// Adds a <see cref="FormletData"/> item identified by a specified name.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="value"></param>
		public void Add(string name, FormletData value)
		{
			if (value.Name == null || value.Name.Length == 0)
			{
				value.Name = name;
			}

			this.List.Add(value);
		}

		/// <summary>
		/// Implements <see cref="IList.Add"/>
		/// </summary>
		/// <param name="item"></param>
		public void Add(object item)
		{
			ArgumentValidator.CheckForExpectedType(item, "item", typeof(FormletData));
			this.List.Add(item);
		}

		/// <summary>
		/// Adds a <see cref="FormletData"/> item to the collection
		/// </summary>
		/// <param name="item"></param>
		public void Add(FormletData item)
		{
			this.List.Add(item);
		}

		/// <summary>
		/// Implements <see cref="IList.Insert"/>
		/// </summary>
		/// <param name="index"></param>
		/// <param name="item"></param>
		public void Insert(int index, FormletData item)
		{
			this.List.Insert(index, item);
		}

		/// <summary>
		/// Removes an item by name
		/// </summary>
		/// <param name="name"></param>
		public void Remove(string name)
		{
			FormletData item = (FormletData)this.dictionary[name];

			if (item != null)
			{
				this.List.RemoveAt(this.List.IndexOf(item));
			}
		}

		/// <summary>
		/// Implements <see cref="IList.IndexOf"/>
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public int IndexOf(FormletData item)
		{
			return List.IndexOf(item);
		}

		/// <summary>
		/// Implements <see cref="IList.Contains"/>
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public bool Contains(FormletData item)
		{
			return List.Contains(item);
		}

		/// <summary>
		/// Implements <see cref="ICollection.CopyTo"/>
		/// </summary>
		/// <param name="array"></param>
		/// <param name="index"></param>
		public void CopyTo(FormletData[] array, int index)
		{
			this.List.CopyTo(array, index);
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnClearComplete"/>
		/// </summary>
		protected override void OnClearComplete()
		{
			this.dictionary.Clear();
			base.OnClearComplete ();
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnInsert"/>
		/// </summary>
		/// <param name="index"></param>
		/// <param name="value"></param>
		protected override void OnInsert(int index, object value)
		{
			ArgumentValidator.CheckForExpectedType(value, "value", typeof(FormletData));

			if (this.List.Contains(value))
			{
				throw new ArgumentException("Invalid attempt to insert an item that already exists");
			}

			base.OnInsert (index, value);
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnInsertComplete"/>
		/// </summary>
		/// <param name="index"></param>
		/// <param name="value"></param>
		protected override void OnInsertComplete(int index, object value)
		{
			base.OnInsertComplete(index, value);

			ArgumentValidator.CheckForExpectedType(value, "value", typeof(FormletData));

			FormletData item = (FormletData)value;

			if (item.Name != null && item.Name.Length > 0)
			{
				this.dictionary.Add(item.Name, item);
			}
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnRemoveComplete"/>
		/// </summary>
		/// <param name="index"></param>
		/// <param name="value"></param>
		protected override void OnRemoveComplete(int index, object value)
		{
			ArgumentValidator.CheckForExpectedType(value, "value", typeof(FormletData));
			
			FormletData item = (FormletData)value;
			
			this.RemoveItem(item);
			base.OnRemoveComplete(index, value);
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnValidate"/>
		/// </summary>
		/// <param name="value"></param>
		protected override void OnValidate(object value)
		{
			ArgumentValidator.CheckForExpectedType(value, "value", typeof(FormletData));
			base.OnValidate (value);
		}

		/// <summary>
		/// Overrides <see cref="CollectionBase.OnSetComplete"/>
		/// </summary>
		/// <param name="index"></param>
		/// <param name="oldValue"></param>
		/// <param name="newValue"></param>
		protected override void OnSetComplete(int index, object oldValue, object newValue)
		{
			base.OnSetComplete(index, oldValue, newValue);

			ArgumentValidator.CheckForExpectedType(oldValue, "oldValue", typeof(FormletData));
			ArgumentValidator.CheckForExpectedType(newValue, "newValue", typeof(FormletData));

			if (oldValue != null)
			{
				FormletData oldItem = (FormletData)oldValue;
				this.RemoveItem(oldItem);
			}

			FormletData newItem = (FormletData)newValue;
			this.dictionary[newItem.Name] = newValue;
		}

		private void RemoveItem(FormletData oldItem)
		{
			int oldIndex = this.dictionary.IndexOfValue(oldItem);

			if (oldIndex >= 0)
			{
				this.dictionary.RemoveAt(oldIndex);
			}
		}
		#endregion
	}
}
